package com.timi_licai.timi_licai_account;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.fragment.CalendarFragment;
import com.timi_licai.timi_licai_account.utils.DateUtils;

import java.util.Calendar;

public class CalendarActivity extends ActionBarActivity {
    private ViewPager viewPager;
    private TextView tvMonth;
    private String month;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        viewPager = (ViewPager) this.findViewById(R.id.viewpager);

        final ScreenSlidePagerAdapter screenSlidePagerAdapter =
                new ScreenSlidePagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(screenSlidePagerAdapter);
        viewPager.setCurrentItem(500);
        tvMonth = (TextView) this.findViewById(R.id.tv_month);
        month = Calendar.getInstance().get(Calendar.YEAR)
                + "-"
                + DateUtils.LeftPad_Tow_Zero(Calendar.getInstance().get(
                Calendar.MONTH) + 1);
        tvMonth.setText(month);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Calendar calendar = DateUtils.getSelectCalendar(position);
                month = calendar.get(Calendar.YEAR)
                        + "-"
                        + DateUtils.LeftPad_Tow_Zero(calendar.get(Calendar.MONTH) + 1);
                tvMonth.setText(month);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return CalendarFragment.create(position);
        }

        @Override
        public int getCount() {
            return 1000;
        }
    }
}
